package com.zhsj.OkeHomeConsumption.ui.activity.citypartner;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.StoreBean;
import com.zhsj.OkeHomeConsumption.empty.StoredetailsTypeBean;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.StoredetailsTypePresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.StoredetailsTypeUIInterFace;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *商家招募--商家维护--查看详情--主营产品
 *
 * **/

public class StoredetailsTypeActivity extends MyActivity implements StoredetailsTypeUIInterFace {

    @BindView(R.id.recy)
    RecyclerView recy;
    private List<StoreBean> mWalletServiceList;

    private List<StoredetailsTypeBean>  shopScopeList;//存储选中的值

    private StoredetailsTypePresenter minePersonPresenter;

    private Button queren_button;
    private Button quxiao_button;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_storedatailstype;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_test_a_bar;
    }

    @Override
    protected void initView() {
        queren_button = findViewById(R.id.queren_button);
        quxiao_button = findViewById(R.id.quxiao_button);

         minePersonPresenter = new StoredetailsTypePresenter(getActivity(),this);
        mWalletServiceList = new ArrayList<>();
        shopScopeList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        recy.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);

    }

    @Override
    protected void initData() {

        minePersonPresenter.MerchantTypeGet();
        queren_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent=new Intent();
                intent.putExtra("list", (Serializable) shopScopeList);
                setResult(5,intent);
                finish();
            }
        });
        quxiao_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void MerchantTypeGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                // 数据对接正常
                mWalletServiceList = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<StoreBean>>() {
                        }.getType());

                ShopAdapter shopadapteer = new ShopAdapter();
                recy.setAdapter(shopadapteer);


            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("MerchantChildTypeGet", "获取邀请的店铺列表");
        }
    }

    public class ShopAdapter extends BaseQuickAdapter<StoreBean, BaseViewHolder> {
        public ShopAdapter() {
            super(R.layout.item_jingyingfanwei, mWalletServiceList);
        }

        @Override
        protected void convert(BaseViewHolder helper, final StoreBean item) {
            helper.setText(R.id.title_text, item.getCategoryName());

            TagFlowLayout mDefaultTagGroup = helper.itemView.findViewById(R.id.id_flowlayout);

            mDefaultTagGroup.setAdapter(new TagAdapter<StoredetailsTypeBean>(item.getShopScopeList()) {
                @Override
                public View getView(FlowLayout parent, int position, StoredetailsTypeBean storedetailsTypeBean) {
                    LayoutInflater inflater = LayoutInflater.from(getActivity());
                    TextView tv = (TextView) inflater.inflate(R.layout.item_zhuyingchanpin,
                            parent, false);
                    tv.setText(storedetailsTypeBean.getBusinessScopeName());
                    return tv;
                }

                @Override
                public void onSelected(int position, View view) {
                    super.onSelected(position, view);
                    view.setBackgroundResource(R.drawable.shape_button);
                }

                @Override
                public void unSelected(int position, View view) {
                    super.unSelected(position, view);
                    view.setBackgroundResource(R.drawable.background_white_round_gray);
                }
            });

            mDefaultTagGroup.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    shopScopeList.add(item.getShopScopeList().get(position));
                    return true;
                }
            });

        }
    }
}
